﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using ByteSite.DBUtility;
using ByteSite.Common;
using SqlSugar;
using System.Linq.Expressions;

namespace ByteSite.DAL
{
    /// <summary>
    /// 数据访问类:系统导航菜单
    /// </summary>
    public partial class navigation
    {
        public SqlSugarClient db;
        public navigation()
        {
            db = new DBUtility.DbContext().Db;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return db.Queryable<Entitys.dt_navigation>().Where(x => x.id == id).Any();
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_navigation model)
        {
            model.id = db.Insertable<Entitys.dt_navigation>(model).ExecuteReturnIdentity();
            return model.id;
        }

        public int Add(string parent_name, string nav_name, string title, string link_url, int sort_id, int channel_id, string action_type)
        {
            Entitys.dt_navigation model = db.Queryable<Entitys.dt_navigation>().Where(x => x.name == parent_name).First();

            return Add(model.id, nav_name, title, link_url, sort_id, channel_id, action_type);
        }

        /// <summary>
        /// 快捷添加系统默认导航
        /// </summary>
        public int Add(int parent_id, string nav_name, string title, string link_url, int sort_id, int channel_id, string action_type)
        {
            return db.Insertable<Entitys.dt_navigation>(new Entitys.dt_navigation()
            {
                parent_id = parent_id,
                channel_id = channel_id,
                nav_type = DTEnums.NavigationEnum.System.ToString(),
                name = nav_name,
                title = title,
                link_url = link_url,
                sort_id = sort_id,
                action_type = action_type,
                is_lock = 0,
                is_sys = 1
            }).ExecuteReturnIdentity();
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_navigation model)
        {
            return db.Updateable(model).ExecuteCommand() > 0;
        }
        /// <summary>
        /// 修改一条记录，带事务
        /// </summary>
        public bool Update(string old_name, string new_name, string title, int sort_id)
        {
            return db.Updateable<Entitys.dt_navigation>(new Entitys.dt_navigation()
            {
                name = new_name,
                title = title,
                sort_id = sort_id
            }).Where(x => x.name == old_name).ExecuteCommand() > 0;
        }

        public bool Update(string old_name, int parent_id, string nav_name, string title, int sort_id)
        {
            return db.Updateable<Entitys.dt_navigation>(new Entitys.dt_navigation()
            {
                parent_id = parent_id,
                name = nav_name,
                title = title,
                sort_id = sort_id
            }).Where(x => x.name == old_name).ExecuteCommand() > 0;
        }
        public bool Update(string old_name, string new_name)
        {
            return db.Updateable<Entitys.dt_navigation>().SetColumns(x => x.name == new_name).Where(x => x.name == old_name).ExecuteCommand() > 0;
            //return db.Updateable<Entitys.dt_navigation>(new Entitys.dt_navigation()
            //{
            //    name = new_name
            //}).Where(x => x.name == old_name).ExecuteCommand() > 0;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return db.Deleteable<Entitys.dt_navigation>().Where(x => x.id == id).ExecuteCommand() > 0;
        }

        public bool Delete(Expression<Func<Entitys.dt_navigation, bool>> whereExpression)
        {
            return db.Deleteable<Entitys.dt_navigation>().Where(whereExpression).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据导航的名称查询其ID
        /// </summary>
        public int GetNavId(string nav_name)
        {
            Entitys.dt_navigation model = db.Queryable<Entitys.dt_navigation>().Where(x => x.name == nav_name).First();
            if (model == null)
                return 0;
            else
                return model.id;
        }

        /// <summary>
        /// 获取父类下的所有子类ID(含自己本身)
        /// </summary>
        public List<int> GetIds(int parent_id)
        {
            List<int> idList = new List<int>();
            List<Entitys.dt_navigation> navList = db.Queryable<Entitys.dt_navigation>().ToList();
            idList.Add(parent_id);
            GetChildIds(navList, parent_id, ref idList);
            return idList;
        }

        /// <summary>
        /// 获取父类下的所有子类ID(含自己本身)
        /// </summary>
        public List<int> GetIds(string nav_name)
        {
            int id = Utils.ObjToInt(db.Queryable<Entitys.dt_navigation>().Where(x => x.name == nav_name).First().id, 0);
            if (id != 0)
            {
                return GetIds(id);
            }
            return new List<int>();
        }





        /// <summary>
        /// 根据父节点ID获取导航列表
        /// </summary>
        public List<Entitys.dt_navigation> GetList(int parent_id, string nav_type)
        {
            List<Entitys.dt_navigation> oldList = db.Queryable<Entitys.dt_navigation>().Where(x => x.nav_type == nav_type).OrderBy(x => x.sort_id, OrderByType.Asc).OrderBy(x => x.id, OrderByType.Desc).ToList();
            if (oldList == null)
            {
                return null;
            }
            List<Entitys.dt_navigation> newList = new List<Entitys.dt_navigation>();
            GetChilds(oldList, newList, parent_id, 0);
            return newList;
        }


        /// <summary>
        /// 从内存中取得所有下级类别列表（自身迭代）
        /// </summary>
        private void GetChilds(List<Entitys.dt_navigation> oldData, List<Entitys.dt_navigation> newData, int parent_id, int class_layer)
        {
            class_layer++;
            foreach (Entitys.dt_navigation nav in oldData.FindAll(x => x.parent_id == parent_id))
            {
                nav.class_layer = class_layer;
                newData.Add(nav);
                //调用自身迭代
                this.GetChilds(oldData, newData, nav.id, class_layer);

            }
        }

        /// <summary>
        /// 获取父类下的所有子类ID
        /// </summary>
        private void GetChildIds(List<Entitys.dt_navigation> list, int parent_id, ref List<int> ids)
        {
            list.Find(x => x.parent_id == parent_id);
            foreach (Entitys.dt_navigation nav in list)
            {
                ids.Add(nav.id);
                this.GetChildIds(list, nav.id, ref ids);
            }
        }
    }
}